import java.util.*;

/**
 * @author LKQ
 * @date 2022/6/19 9:48
 * @description
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        TreeNode root = new TreeNode(1, new TreeNode(1), null);
        solution.findFrequentTreeSum(root);
    }
    Map<Integer, Integer> map = new HashMap<>();
    int max = 0;
    public int[] findFrequentTreeSum(TreeNode root) {
        getSum(root);
        List<Integer> ans = new ArrayList<>();
        for(Integer a: map.keySet()) {
            if(map.get(a) == max) {
                ans.add(a);
            }
        }
        int[] res = new int[ans.size()];
        for (int i = 0; i < ans.size(); i++) {
            res[i] = ans.get(i);
        }
        return res;
    }
    // 获取子树的和
    public int getSum(TreeNode node) {
        if(node == null) {
            return 0;
        }
        int sum = node.val + getSum(node.left) + getSum(node.right);
        map.put(sum, map.getOrDefault(sum, 0) + 1);
        if(map.get(sum) > max) {
            max = map.get(sum);
        }
        return sum;
    }
}
